package com.aaa.cs.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.dao.DictDataDao;
import com.aaa.cs.entity.DictData;
import com.aaa.cs.service.DictDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 字典数据表(DictData)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 18:02:21
 */
@Service("dictDataService")
public class DictDataServiceImpl implements DictDataService {
    @Resource
    private DictDataDao dictDataDao;

    /**
     * 通过ID查询单条数据
     *
     * @param dictCode 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer dictCode) {
        if(dictCode == null){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }

        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),this.dictDataDao.queryById(dictCode));
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param dictData 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, DictData dictData) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<DictData> pageInfo = new PageInfo<DictData>(dictDataDao.queryAll(dictData));
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    @Override
    public Result<DictData> query() {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),this.dictDataDao.query());
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public Result queryAllByLimit(int offset, int limit) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),this.dictDataDao.queryAllByLimit(offset, limit));
    }

    /**
     * 新增数据
     *
     * @param dictData 实例对象
     * @return 实例对象
     */
    @Override
    public DictData insert(DictData dictData) {
        this.dictDataDao.insert(dictData);
        return dictData;
    }

    /**
     * 修改数据
     *
     * @param dictData 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(DictData dictData) {
        dictData.setUpdateTime(new Date());
        this.dictDataDao.update(dictData);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),this.queryById(dictDataDao.update(dictData)));
        //this.queryById(dictData.getDictCode())
    }

    /**
     * 通过主键删除数据
     *
     * @param dictCode 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Integer dictCode) {
        if(dictCode == null){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode()
                    ,ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnStatus.SUCCESS.getReturnCode()
                ,ReturnStatus.SUCCESS.getReturnMsg(),this.dictDataDao.deleteById(dictCode)>0);
    }


}